﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TwitterNameTableAdapters;
//using System.DateTime;


public partial class _Default : System.Web.UI.Page
{
            int intuserid;
            const int ANONIMUSER = -1; const int ALLUSER = 0;
            MessagesTableAdapter MessageAdapter = new MessagesTableAdapter();
            UsersTableAdapter UserAdapter = new UsersTableAdapter();

    protected void Page_Load(object sender, EventArgs e)
    { 
        int num = Convert.ToInt16(Request.QueryString["id"]);

        // 
        

        Button1.Enabled = true;
        if (!this.IsPostBack)
        {
            #region NickNameBox -DropDownList control for adresat

            NickNameBox.Items.Clear();
            // Specify the data source and field names for the Text 
            // and Value properties of the items (ListItem objects) 
            // in the DropDownList control.
            NickNameBox.DataSource = UserAdapter.GetData()/*CreateDataSource()*/;
            NickNameBox.DataTextField = "nick";
            NickNameBox.DataValueField = "id_users";

            // Bind the data to the control.
            NickNameBox.DataBind();

            // Set the default selected item, if desired.
            NickNameBox.SelectedIndex = 0;
            #endregion
            string userid1 = HttpContext.Current.User.Identity.Name.ToString();

            try
            {
                //ListView1.DataSource = MessageAdapter.GetDataBy_IDusers(intuserid);
               // ListView1.DataSourceID = "MessageAdapter";
                if (userid1 == "")
                {
                    Button1.Enabled = false;
                    GridView1.DataSource = MessageAdapter.GetDataBy_id_for_user(ANONIMUSER);
                }
                else
                {
                    Button1.Enabled = true;

                    intuserid = (int)UserAdapter.ScalarQuery_getUserID(userid1);//Response.Write(/*"Значение параметра id = "+*/ intuserid.ToString);
                    if (num == null)
                    {
                        GridView1.DataSource = MessageAdapter.GetDataBy_IDusers(intuserid);     //GridView1.DataSource = MessageAdapter.GetDataBy_IDusers(intuserid);
                    }
                    else {
                        NickNameBox.Text = (string)UserAdapter.ScalarQueryNickbyID(num);
                        GridView1.DataSource = MessageAdapter.GetDataBy_IDusers(intuserid); 
                    }
                }
                #region GridView1 - configuring view design
                GridView1.BackColor = System.Drawing.Color.DodgerBlue;
                GridView1.ForeColor = System.Drawing.Color.Snow;
                GridView1.CellPadding = 0;
                GridView1.CellSpacing = 1;
                GridView1.BorderColor = System.Drawing.Color.SkyBlue;
                GridView1.BorderWidth = 1;
                GridView1.ShowHeader = false; // GridView1.ShowHeader ? false : true;
                GridView1.DataBind();
                //Response.Write("Значение параметра id = " + Request.QueryString["id"]);
                #endregion
            }
            catch (Exception)
            {

                //string msg = "";
                //   for (int i = 0; i < e.Errors.Count; i++)
                //   {
                //       msg += "Error #" + i + " Message: " + e.Errors[i].Message + "\n";
                //   }
                //   System.Console.WriteLine(msg);
                ;
            }
        }

    }
    protected void ManageProducts_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {

    }

    protected void C(object sender, DetailsViewPageEventArgs e)
    {

    }


    public void InsertUser(object source, EventArgs e)
    {
        string userid1; userid1 = HttpContext.Current.User.Identity.Name.ToString();
        try
        {
            MessagesTableAdapter MessageAdapter1 = new MessagesTableAdapter();
            int tmp = 0;
            if (UserAdapter.ScalarQuery_getUserID(NickNameBox.Text) != null)
            {
                tmp = (int) UserAdapter.ScalarQuery_getUserID(NickNameBox.Text);

            }
            MessageAdapter1.InsertQueryMSG((int) MessageAdapter1.ScalarQuery_MAXID() + 1, NameBox.Text, intuserid = (int)UserAdapter.ScalarQuery_getUserID(userid1), tmp, DateTime.Now);
        }
        catch (Exception) { Label2.Text = "Failed "; Label2.ForeColor = System.Drawing.Color.Red; }
        finally { Label2.Text = "Posted "; Label2.ForeColor = System.Drawing.Color.Green; }

         userid1 = HttpContext.Current.User.Identity.Name.ToString();
        
        try
        {
            if (userid1 == "")
            {
                
                GridView1.DataSource = MessageAdapter.GetDataBy_id_for_user(ANONIMUSER);
            }
            else
            {
                intuserid = (int)UserAdapter.ScalarQuery_getUserID(userid1);
                GridView1.DataSource = MessageAdapter.GetDataBy_IDusers(intuserid);
                //GridView1.DataSource = MessageAdapter.GetDataBy_id_for_user(intuserid);
                // GridView1.DataSource = MessageAdapter.GetData();
                //Response.Write(Convert.ToString(intuserid));
            }

            GridView1.BackColor = System.Drawing.Color.DodgerBlue;
            GridView1.ForeColor = System.Drawing.Color.Snow;
            GridView1.CellPadding = 0;
            GridView1.CellSpacing = 1;
            GridView1.BorderColor = System.Drawing.Color.SkyBlue;
            GridView1.BorderWidth = 1;
            GridView1.ShowHeader = GridView1.ShowHeader ? false : true;
            GridView1.DataBind();
            //Response.Write("Значение параметра id = " + Request.QueryString["id"]);
        }
        catch (Exception)
        {
            //string msg = "";
            //   for (int i = 0; i < e.Errors.Count; i++)
            //   {
            //       msg += "Error #" + i + " Message: " + e.Errors[i].Message + "\n";
            //   }
            //   System.Console.WriteLine(msg);
            ;
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string userid1;
        userid1 = HttpContext.Current.User.Identity.Name.ToString();

        try
        {
            if (userid1 == "")
            {

                GridView1.DataSource = MessageAdapter.GetDataBy_id_for_user(ANONIMUSER);
            }
            else
            {
                intuserid = (int)UserAdapter.ScalarQuery_getUserID(userid1);
                GridView1.DataSource = MessageAdapter.GetDataBy_LIKE("%"+id_user.Text+"%",intuserid);
                //   Response.Write(Convert.ToString(intuserid));
            }

            GridView1.BackColor = System.Drawing.Color.DodgerBlue;
            GridView1.ForeColor = System.Drawing.Color.Snow;
            GridView1.CellPadding = 0;
            GridView1.CellSpacing = 1;
            GridView1.BorderColor = System.Drawing.Color.SkyBlue;
            GridView1.BorderWidth = 1;
            GridView1.ShowHeader = GridView1.ShowHeader ? false : true;
            GridView1.DataBind();
            //Response.Write("Значение параметра id = " + Request.QueryString["id"]);
        }
        catch (Exception)
        {
            //string msg = "";
            //   for (int i = 0; i < e.Errors.Count; i++)
            //   {
            //       msg += "Error #" + i + " Message: " + e.Errors[i].Message + "\n";
            //   }
            //   System.Console.WriteLine(msg);
            ;
        }
    }
}

    